#include <bits/stdc++.h>
// 2025/02/06
// tag: Dynamic Programming
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

#define int long long

int K, V, N, dp[5001][51], w[201], v[201], mer[51];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(dp, 128, sizeof(dp));
    cin >> K >> V >> N;
    for (int i = 1; i <= N; i++)
        cin >> w[i] >> v[i];
    dp[0][1] = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = V; j >= w[i]; j--) {
            int x = 1, y = 1, z = 1;
            while (x + y <= K + 1) {
                if (dp[j - w[i]][x] + v[i] > dp[j][y])
                    mer[z] = dp[j - w[i]][x] + v[i], x++;
                else
                    mer[z] = dp[j][y], y++;
                z++;
            }
            for (int k = 1; k <= K; k++)
                dp[j][k] = mer[k];
        }
    }
    int presum = 0;
    for (int i = 1; i <= K; i++)
        presum += dp[V][i];
    cout << presum << endl;
    return 0;
}
